<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>表单提交+正则表达式</title>
</head>

<body>
    <form action="" name="loginForm" method="post">
        <div>
            <label for="phoneInput">手机：</label>
            <input type="text" id="phoneInput" name="phone" placeholder="请输入手机" required pattern="^[0-9]{11}$">
            <!-- required html5 才有的属性 为考虑兼容性js最好再做字符存在性判断 if(!phone.length) -->
            <!-- pattern="/^[0-9]{11}$/" 也需要js if (!/^[0-9]{11}$/.test(phone)) 双保险 -->
        </div>
        <div>
            <button type="submit">提交</button>
        </div>
    </form>
    <script>
        const oForm = document.forms["loginForm"];
        oForm.addEventListener('submit', function (event) {
            event.preventDefault();
            const phone = oForm["phone"].value.trim();
            if (!phone.length) {
                console.log("手机号不能为空");
                return;
            }
            if (!/^[0-9]{11}$/.test(phone)) {
                console.log("手机号格式有误");
                return;
            }
            // console.log(phone);
            this.submit();
        })
    </script>
</body>

</html>